#include <QtGui>
#include "ControlModel.h"
#include "ControlModelItem.h"
#include "rpi_control.h"

ControlModel::ControlModel(QList<rpi_control*> *c, QObject *parent)
    : QAbstractItemModel(parent)
{
    QList<QVariant> rootData;
    rootData << "Id" << "Type" << "Device" << "Address" << "Node" << "Value";
    rootItem = new ControlItem(rootData);
    setupModelData(c, rootItem);
}

ControlModel::~ControlModel()
{
    delete rootItem;
}

int ControlModel::columnCount(const QModelIndex &parent) const
{
    if (parent.isValid())
        return static_cast<ControlItem*>(parent.internalPointer())->columnCount();
    else
        return rootItem->columnCount();
}

QVariant ControlModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    //if (role != Qt::DisplayRole)
    //    return QVariant();

    switch (role) {
        case Qt::DisplayRole : {
            ControlItem *item = static_cast<ControlItem*>(index.internalPointer());
            return item->data(index.column());
        }
        case Qt::EditRole : {

        }

        default : { return QVariant(); }
    }

    ControlItem *item = static_cast<ControlItem*>(index.internalPointer());

    return item->data(index.column());
}

Qt::ItemFlags ControlModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}

QVariant ControlModel::headerData(int section, Qt::Orientation orientation,
                               int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
        return rootItem->data(section);

    return QVariant();
}

QModelIndex ControlModel::index(int row, int column, const QModelIndex &parent)
            const
{
    if (!hasIndex(row, column, parent))
        return QModelIndex();

    ControlItem *parentItem;

    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast<ControlItem*>(parent.internalPointer());

    ControlItem *childItem = parentItem->child(row);
    if (childItem)
        return createIndex(row, column, childItem);
    else
        return QModelIndex();
}

QModelIndex ControlModel::parent(const QModelIndex &index) const
{
    if (!index.isValid())
        return QModelIndex();

    ControlItem *childItem = static_cast<ControlItem*>(index.internalPointer());
    ControlItem *parentItem = childItem->parent();

    if (parentItem == rootItem)
        return QModelIndex();

    return createIndex(parentItem->row(), 0, parentItem);
}

int ControlModel::rowCount(const QModelIndex &parent) const
{
    ControlItem *parentItem;
    if (parent.column() > 0)
        return 0;

    if (!parent.isValid())
        parentItem = rootItem;
    else
        parentItem = static_cast<ControlItem*>(parent.internalPointer());

    return parentItem->childCount();
}

void ControlModel::setupModelData(QList<rpi_control*> *c, ControlItem *parent)
{
    if (!c) return;
    QList<ControlItem*> parents;
    QList<int> indentations;
    parents << parent;
    indentations << 0;

    for (int k=0;k<c->size();k++) {
        ControlItem *n= new ControlItem((*c)[k],parent);
        parent->appendChild(n);
    }

    /*int number = 0;

    while (number < c->size()) {
        int position = 0;
        while (position < lines[number].length()) {
            if (lines[number].mid(position, 1) != " ")
                break;
            position++;
        }

        QString lineData = lines[number].mid(position).trimmed();

        if (!lineData.isEmpty()) {
            // Read the column data from the rest of the line.
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
            QList<QVariant> columnData;
            for (int column = 0; column < columnStrings.count(); ++column)
                columnData << columnStrings[column];

            if (position > indentations.last()) {
                // The last child of the current parent is now the new parent
                // unless the current parent has no children.

                if (parents.last()->childCount() > 0) {
                    parents << parents.last()->child(parents.last()->childCount()-1);
                    indentations << position;
                }
            } else {
                while (position < indentations.last() && parents.count() > 0) {
                    parents.pop_back();
                    indentations.pop_back();
                }
            }

            // Append a new item to the current parent's list of children.
            parents.last()->appendChild(new ControlItem(columnData, parents.last()));
        }

        number++;
    }*/
}
